package com.bhkj.pdjhbgnewproject.core.activity;

import android.Manifest;
import android.arch.lifecycle.ViewModelProviders;
import android.widget.TextView;

import com.bhkj.pdjhbgnewproject.R;
import com.bhkj.pdjhbgnewproject.common.contanct.Contacts;
import com.bhkj.pdjhbgnewproject.core.base.BaseActivity;
import com.bhkj.pdjhbgnewproject.core.main.MainActivity;
import com.bhkj.pdjhbgnewproject.core.service.ObtainPdInfoService;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BusUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ServiceUtils;

/**
 * 叫号器用来绑定窗口屏，需要显示相应的ip
 */
public class BindIpActivity extends BaseActivity {
    private TextView bindIpTv;

    @Override
    public int getLayoutId() {
        return R.layout.activity_bind_tip;
    }

    @Override
    public void initView() {
        PermissionUtils permission = PermissionUtils.permission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE});
        permission.request();
        permission.callback(new PermissionUtils.SimpleCallback() {
            @Override
            public void onGranted() {

            }

            @Override
            public void onDenied() {

            }
        });
        bindIpTv = findViewById(R.id.tv_bind_tip);
    }

    @Override
    public void initData() {
//        UpdateUtil.checkUpdate(this, false);
        ServiceUtils.startService(ObtainPdInfoService.class);

        SPUtils instance = SPUtils.getInstance();
        String hasBindDevice = instance.getString(Contacts.BIND_ALIAS);
        if (ObjectUtils.isNotEmpty(hasBindDevice)) {
            ActivityUtils.startActivity(MainActivity.class);
            ActivityUtils.finishActivity(BindIpActivity.class);
        }

    }


    /**
     * 显示绑定ip
     *
     * @param ip ip地址
     */
    @BusUtils.Bus(tag = Contacts.PARAM_BIND_IP, sticky = true)
    public void showBindIp(final String ip) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                bindIpTv.setText("需要在叫号器上输入下面的ip绑定窗口屏\n" + ip);
            }
        });
    }

}
